package cn.tedu.trading_platform.controller;

import cn.tedu.trading_platform.base.response.JsonResult;
import cn.tedu.trading_platform.base.response.StatusCode;
import cn.tedu.trading_platform.mapper.AdminMapper;
import cn.tedu.trading_platform.pojo.dto.adminDTO.AdminAddParam;
import cn.tedu.trading_platform.pojo.dto.adminDTO.AdminLoginParam;
import cn.tedu.trading_platform.pojo.dto.adminDTO.UserStatusUpdateParam;
import cn.tedu.trading_platform.pojo.vo.AdminOrderVO;
import cn.tedu.trading_platform.pojo.vo.AdminUserVO;
import cn.tedu.trading_platform.pojo.vo.AdminVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/")
@Api(tags = "管理员模块")
public class AdminController {
    @Autowired
    private AdminMapper adminMapper;
    //管理员登录
    @PostMapping("login")
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 1)
    public JsonResult adminLogin(@Validated @RequestBody AdminLoginParam adminLoginParam, HttpSession session) {
        log.debug("开始处理管理员登录请求");
        AdminVO adminVO = adminMapper.selectByAdminNameAndPassword(adminLoginParam);
        if(adminVO==null){
            return new JsonResult(StatusCode.USER_NOT_EXIST_OR_PASSWORD_ERROR);
        }
        session.setAttribute("admin",adminVO);
        return JsonResult.ok();
    }

    //管理员查询用户信息
    @PostMapping("adminUser")
    @ApiOperation("管理员查询用户信息")
    @ApiOperationSupport(order = 2)
    public JsonResult adminUser(HttpSession session){
        log.debug("开始处理管理员查询用户信息");
        //1.验证管理员是否登录
        AdminVO adminVO=(AdminVO) session.getAttribute("admin");
        if (adminVO==null){
            return new JsonResult(StatusCode.USER_NOT_LOGIN);
        }
        //2.查询用户信息列表
        List<AdminUserVO> userVOS=adminMapper.adminUser();
        return JsonResult.ok(userVOS);
    }

    //管理员查询订单信息
    @PostMapping("adminOrder")
    @ApiOperation("管理员查询订单信息")
    @ApiOperationSupport(order = 3)
    public JsonResult adminOrder(HttpSession session){
        log.debug("开始处理管理员查询订单信息");
        //1.验证管理员是否登录
        AdminVO adminVO=(AdminVO) session.getAttribute("admin");
        if (adminVO==null){
            return new JsonResult(StatusCode.USER_NOT_LOGIN);
        }
        //2.查询订单信息列表
        List<AdminOrderVO> orderVOS=adminMapper.adminOrder();
        return JsonResult.ok(orderVOS);
    }

    //添加管理员
    @PostMapping("addAdmin")
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 4)
    public JsonResult addAdmin(@Validated @RequestBody AdminAddParam adminAddParam,HttpSession session){
        log.debug("开始处理管理员的请求");
        //1.校验当前管理员权限
        AdminVO currentAdmin=(AdminVO) session.getAttribute("admin");
        if (currentAdmin == null ){
            return new JsonResult(StatusCode.USER_NOT_LOGIN);
        }
        //2.校验账号唯一性
        if (adminMapper.existsByAdminname(adminAddParam.getAdminname())){
            return new JsonResult(StatusCode.USERNAME_ALREADY_EXISTS);
        }
        //3.插入数据库(示例中密码明文存储，实际需要加密)
        adminMapper.insertAdmin(adminAddParam);
        return JsonResult.ok();
    }

    //修改用户状态(禁用/启用用户账号)
    @PostMapping("updateUserStatus")
    @ApiOperation("修改用户状态")
    @ApiOperationSupport(order = 5)
    public JsonResult updateUserStatus(@Validated @RequestBody UserStatusUpdateParam userStatusUpdateParam, HttpSession session) {
        log.debug("开始处理用户状态修改请求");
        AdminVO adminVO = (AdminVO) session.getAttribute("admin");
        if (adminVO == null) {
            return new JsonResult(StatusCode.USER_NOT_LOGIN);
        }
        int i = adminMapper.updateUserStatus(userStatusUpdateParam);
        if (i == 0) {
            return new JsonResult(StatusCode.USER_NOT_EXIST_OR_PASSWORD_ERROR_OR_NOT_ADMIN_OR_NOT_LOGIN);
        }
        return JsonResult.ok();
    }
}